package com.dzt.androidframework.changeskin.lib;

import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;

import com.dzt.androidframework.changeskin.lib.utils.L;

/**
 * Created by zhy on 15/9/22.
 * 得到插件的资源文件：图片、颜色
 */
public class ResourceManager {
	private static final String DEFTYPE_DRAWABLE = "drawable";
	private static final String DEFTYPE_COLOR = "color";
	private Resources mResources;
	private String mPluginPackageName;   //插件包名
	private String mSuffix;

	public ResourceManager(Resources res, String pluginPackageName, String suffix) {
		mResources = res;
		mPluginPackageName = pluginPackageName;

		if (suffix == null) {
			suffix = "";
		}
		mSuffix = suffix;
	}

	public Drawable getDrawableByName(String name) {
		try {
			name = appendSuffix(name);
			//L.e("name = " + name);
			return mResources.getDrawable(mResources.getIdentifier(name, DEFTYPE_DRAWABLE, mPluginPackageName));
		} catch (Resources.NotFoundException e) {
			try {
				return mResources.getDrawable(mResources.getIdentifier(name, DEFTYPE_COLOR, mPluginPackageName));
			} catch (Resources.NotFoundException e2) {
				e.printStackTrace();
				return null;
			}
		}
	}

	public int getColor(String name) {
		try {
			name = appendSuffix(name);
			L.e("name = " + name);
			return mResources.getColor(mResources.getIdentifier(name, DEFTYPE_COLOR, mPluginPackageName));
		} catch (Resources.NotFoundException e) {
			e.printStackTrace();
			return -1;
		}
	}

	public ColorStateList getColorStateList(String name) {
		try {
			name = appendSuffix(name);
			L.e("name = " + name);
			return mResources.getColorStateList(mResources.getIdentifier(name, DEFTYPE_COLOR, mPluginPackageName));
		} catch (Resources.NotFoundException e) {
			e.printStackTrace();
			return null;
		}
	}

	private String appendSuffix(String name) {
		if (!TextUtils.isEmpty(mSuffix))
			return name += "_" + mSuffix;
		return name;
	}
}
